package train.DAY624;

import train.DAY623.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test624 {
    //反射练习
    public static void main1(String[] args) throws Exception {
        Class clazz = Student.class;
        Class<?> aClass = Class.forName("train.DAY623.Student");

        //反射动态创建对象
        Field[] fields = clazz.getDeclaredFields();
        Method[] methods = clazz.getDeclaredMethods();
        Student instance = (Student) clazz.newInstance();//创建无参对象
        Student o1 = (Student) clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});//创建无参对象

        //创建有参对象
        Constructor constructor = clazz.getConstructor(new Class[]{String.class, int.class, Double.class, Double.class});
        Student o = (Student) constructor.newInstance(new Object[]{"李", 34, 56.0, 78.0});
        //System.out.println(o);
//-----------------
        //反射执行方法
        Student instance1 = (Student) clazz.newInstance();
        Object o2 = clazz.newInstance();

        Method setName = clazz.getMethod("setName", new Class[]{String.class});
        Object invoke = setName.invoke(instance1, new Object[]{"大王"});

        System.out.println(instance1.getName());
        Student o3 = (Student) aClass.newInstance();

    }

    public static Integer money = 10000;
    static Semaphore semaphore = new Semaphore(1);
     static Lock lock=new ReentrantLock();

    public static void main(String[] args) {
//        Customer c1 = new Customer();
//        Thread t1 = new Thread(c1);
//        t1.setName("女方");
//        t1.start();
//
//        Customer c2 = new Customer();
//        Thread t2 = new Thread(c2);
//        t2.setName("男方");
//        t2.start();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,
                5,
                1L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        for (int i = 1; i < 6; i++) {
            threadPoolExecutor.execute(new Task());

        }threadPoolExecutor.shutdown();
    }
}
class Task implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}
    class Customer implements Runnable {
        /**
         * 100
         * A: 50   100-50 =50
         * B:  51   50-51
         */

        @Override
        public  void run() {  //200  a150   |200  b60

            // c1  c2  ===(s1   s2 )   c1(s1)   c2(s2) 死锁
            //同步语句块  500  c1=400   stop   c2 =300

                while (Test624.money > 0) {

                   Test624.lock.lock();
                    // synchronized (Test624.money) {  //必须保证锁定全局唯一的对象
                    int pay = new Random().nextInt(1000);
                    if (pay <= Test624.money) {//足够支付

                        try { //结账需要时间
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        Test624.money -= pay;


                        System.out.printf(Thread.currentThread().getName() + "本此消费%d,卡余额%d  \r\n", pay,Test624.money);


                    } else {//不够支付
                        System.out.printf(Thread.currentThread().getName() + "本此计划消费%d,卡余额%d  \r\n", pay, Test624.money);
                    }
                Test624.lock.unlock();
               // }
            }

        }
    }



